package com.franklin.validation.core.utils;


import com.franklin.validation.core.constants.Constants;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;


public abstract class ReflectionUtil {

    /**
     * <p>
     * 反射对象获取泛型
     * </p>
     *
     * @param clazz 对象
     * @param index 泛型所在位置
     * @return Class
     */
    public static Class<?> getSuperClassGenericType(final Class<?> clazz, final int index) {
        Type genType = clazz.getGenericSuperclass();
        if (!(genType instanceof ParameterizedType)) {
            return Object.class;
        }
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        if (index >= params.length || index < 0) {
            return Object.class;
        }
        if (!(params[index] instanceof Class)) {
            return Object.class;
        }
        return (Class<?>) params[index];
    }

    public static Class getInterfaces(Class clazz){
        if (Map.class.isAssignableFrom(clazz)){
            return Map.class;
        }
        return clazz;
    }

    public static boolean isSystemDefaultValue(String defaultVal) {
        return Constants.DEFAULT_VALUE.equals(defaultVal);
    }

}
